# name : ggplot2 swimlane template
# key : ggplot2.swimlane.template
# key : swimlane.template#ggplot2
# contributor: Shuguang Sun
# --
swimlanedt

swimlane_point <- swimlanedt |>
  select(SUBJID, id, suvmaxbsl, ARM, ARMNAM, FSTCRPR, FSTPD, FSTNEWL) |>
  tidyr::pivot_longer(cols = c(FSTCRPR, FSTPD, FSTNEWL),
                      names_to = "responseType", names_prefix = NULL, names_sep = NULL,
                      values_to = "value", values_drop_na = TRUE)

unicode = list(triangle = sprintf('\u25B2'),
               circle = sprintf('\u25CF'),
               square = sprintf('\u25A0'),
               arrow = sprintf('\u2794'))

library(viridis)
library(hrbrthemes)

ggplot(swimlanedt, aes(x = reorder(as.factor(id), DURDAY), y = DURDAY)) +
  geom_bar(stat = "identity", aes(fill = ARM)) +
  geom_point(data=swimlane_point, size = 5, # Use df.shapes to add reponse points
             aes(id, value,
                 colour=responseType, shape=responseType),
             na.rm = TRUE) +
  coord_flip() +
  scale_fill_manual(values = RColorBrewer::brewer.pal(7, "Pastel2"),
                    labels = c("5x10^5", "1x10^6", "3x10^6", "Ext:1x10^6")) +
  scale_colour_manual(values=RColorBrewer::brewer.pal(3, "Dark2")[1:3],
                      labels = c("First CR/PR", "First New Lesion", "First PD")) + # Add colours
  scale_shape_manual(values=c(unicode[["arrow"]], # Add shapes
                              unicode[["circle"]],
                              unicode[["square"]]),
                      labels = c("First CR/PR", "First New Lesion", "First PD")) +
  ## scale_y_continuous(limits=c(-0.5, 20), breaks=0:20) + # Set time limits
  labs(fill="剂量组", colour="缓解情况", shape="缓解情况",
       x="受试者编号", y="时间（天）"
       ## title="Swimmer Plot"
       ) +
  theme_ipsum_ps(
    ## base_family = font_rc,
    axis_title_just = "c",
    axis_title_size = 11.5,
    ## grid = "X",
    ## axis_title_family = font_rc
    ) +
  theme(legend.position = 'bottom', legend.box = "vertical",
        legend.key.size=unit(0.5, "cm"),
        legend.text=element_text(size=8),
        legend.title=element_text(size=8),
        legend.spacing=unit(0.05, "lines"))
